import axios from 'axios';
axios.defaults.timeout = 5000;  //超市时间是5秒
axios.defaults.withCredentials = true;  //允许跨域
//Content-Type 响应头
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
//基础url
//周本胜
//axios.defaults.baseURL = "http://10.103.173.62:9007";
//服务器环境
 axios.defaults.baseURL = process.env.VUE_APP_BASE_API;
/* axios.defaults.baseURL = window.location.protocol+"//"+window.location.hostname+":9007"; */
/* axios.defaults.baseURL = "/api"; */
//console.log(axios.defaults.baseURL)
//响应拦截器
axios.interceptors.response.use(
    response => {
        //如果reponse里面的status是200，说明访问到接口了，否则错误
        if (response.status == 200) {
            return Promise.resolve(response);
        } else {
            return Promise.reject(response);
        }
    },
    error => {
        if (error.response.status) {
            switch (error.response.status) {
                case 401:       //未登录
                    console.log('401')
                    break;
                case 404:   //没找到
                    break;
            }
            return Promise.reject(error.response);
        }
    }
);

/**
 * 封装get方法
 */
export function get(url, params = {}) {
    return new Promise((resolve, reject) => {
        axios.get(url, { params: params })
            .then(response => {
                resolve(response.data);
            })
            .catch(err => {
                reject(err);
            })
    });
}

/**
 * 封装post方法
 */
export function post(url, data = {}) {
    console.log(axios.defaults.headers)
    return new Promise((resolve, reject) => {
        axios.post(url, data,{headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'}})
            .then(response => {
            
                resolve(response.data);
            })
            .catch(err => {
                reject(err);
            })
    });
}
